//
//  AuctionCountView.swift
//  LeGou
//
//  Created by lxm on 2022/10/15.
//

import UIKit

class AuctionCountView: ZMXIBView {
    
    //MARK: --------------------属性--------------------
    var endTime = Date()  {
        didSet {
            self.update()
        }
    }
    
    let timer = Observable<Int>.timer(.seconds(0), period: .seconds(1), scheduler: MainScheduler.instance)
        
    var onCountOver : (()->())?
    //MARK: --------------------UI控件--------------------
    @IBOutlet weak var h: UIButton!
    
    @IBOutlet weak var m: UIButton!
    
    @IBOutlet weak var s: UIButton!
    //MARK: --------------------生命周期--------------------
    override init(frame: CGRect) {
        super.init(frame: frame)
//        setUI()
//        bindAction()
//        addConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.setUI()
        self.bindAction()
        self.addConstraints()
    }
    
    //MARK: --------------------UI--------------------
    func setUI() {
        
    }
    
    func addConstraints() {
        
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        disposeBag = DisposeBag()
        timer.subscribe(onNext:{ [unowned self] resp in
            self.update()
        }).disposed(by: disposeBag)
    }
    
    func update() {
        let countInfo = AppHelper.countTime(now: getCurrentTimeInterval(), end: self.endTime.timeIntervalSince1970)
        self.h.setTitle(String(format: "%02d", countInfo.h), for: .normal)
        self.m.setTitle(String(format: "%02d", countInfo.m), for: .normal)
        self.s.setTitle(String(format: "%02d", countInfo.s), for: .normal)
        if countInfo.s == 0, countInfo.m == 0, countInfo.h == 0 {
            if let onCountOver = onCountOver {
                onCountOver()
            }
        }
    }
    
    func getCurrentTimeInterval() -> Double {
        return Date().timeIntervalSince1970
    }
    
    
    //MARK: --------------------delegate--------------------

}
